import { G_S } from './config/g_s';
import BaseScene from './core/BaseScene';
import PopupManager from './core/PopupManager'
import SceneNavigator from './core/SceneNavigator';
import { UIManager } from './core/UIManager';
import PlayerData from './data/PlayerData';
import { Commands } from './net/Commond';
import { HttpRequest } from './net/HttpRequest'
import BrowserUtil from './utils/BrowserUtil';
import { FileUtils } from './utils/FileUtil';
import ImageUtil from './utils/ImageUtil';
import LogUtil from './utils/LogUtil';
import NodeUtil from './utils/NodeUtil';
import ObjectUtil from './utils/ObjectUtil';
import RegexUtil from './utils/RegexUtil';
import { StorageUtil } from './utils/StorageUtil';
const { ccclass, property } = cc._decorator;

@ccclass
export default class login extends BaseScene {
    @property(cc.EditBox) edx_username: cc.EditBox = null
    @property(cc.EditBox) edx_password: cc.EditBox = null
   
    onLoad() {
        super.onLoad();
        SceneNavigator.setHome('loginScene');
        let local_username = StorageUtil.ins().getItem('local_username');
        let local_password = StorageUtil.ins().getItem('local_password');
        local_username && (this.edx_username.string = local_username);
        local_password && (this.edx_password.string = local_password);
    }

    protected start(): void {
        //test
        LogUtil.log(ObjectUtil.isNull({2:1}));

        // ParseData.ins().test();
        // UIManager.ins().show('prefabs/UIGuide');
        // StorageUtil.ins().setItem('local_groupId',null);

        let xx = RegexUtil.isIdNumber('341023199105172015');
        let age = RegexUtil.getIDCardAge('341023199105172015');
        LogUtil.log('xx',xx,age);
    }

    onLoginClick() {
        let username = this.edx_username.string;
        let password = this.edx_password.string;
        if (username.length < 4) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1032);
            return;
        }

        if (password.length < 6) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1033);
            return;
        }
        this.loginUser(username, password);
    }

    loginUser(username, password) {
        UIManager.ins().showUILoading(0);
        let httpPromise = HttpRequest.ins().httpPromise(Commands.login, { username: username, password: password }, 'get', () => {
            LogUtil.error(LogUtil.getFileName(),'错误处理1');
            UIManager.ins().hideUILoading();
        })
        httpPromise.then((data:any) => {
            PlayerData.ins().load(data.data);
            PopupManager.show('prefabs/PopBox', {
                title: '提示',
                content: '登录成功',
                confirmCallback: () => {
                    LogUtil.log(LogUtil.getFileName(),'关闭弹窗');
                    StorageUtil.ins().setItem('local_username', username);
                    StorageUtil.ins().setItem('local_password', password);
                    SceneNavigator.go('chatScene', {});
                }
            })
        }).catch((reason:any) => {
            LogUtil.error(LogUtil.getFileName(),'错误处理2',reason);
            UIManager.ins().hideUILoading();
        })
    }

    onRegisterClick() {
        let username = this.edx_username.string;
        let password = this.edx_password.string;
        
        if (username.length < 4) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1032);
            return;
        }

        if (password.length < 6) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1033);
            return;
        }

        if (username == "") {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1019);
            return;
        }

        if (password == "") {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1020);
            return;
        }

        // if (password2 == "") {
        //     UIManager.ins().showMessage(G_S.REGISTER_JS_1021);
        //     return;
        // }

        if (username.indexOf(" ") != -1) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1022);
            return;
        }

        if (RegexUtil.checkChina(username)) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1023);
            return;
        }

        if (password.indexOf(" ") != -1) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1024);
            return;
        }

        // if (password != password2) {
        //     UIManager.ins().showMessage(G_S.REGISTER_JS_1025);
        //     return;
        // }

        if (RegexUtil.hasSpecial(username, "_")) { //不能含有特殊字符但是允许下划线
            UIManager.ins().showMessage(G_S.REGISTER_JS_1034);
            return;
        }

        if (RegexUtil.checkChina(password)) {
            UIManager.ins().showMessage(G_S.REGISTER_JS_1026);
            return;
        }

        this.registerUser(username, password);
    }

    registerUser(username, password) {
        UIManager.ins().showUILoading(0);
        let httpPromise = HttpRequest.ins().httpPromise(Commands.reg, { username: username, password: password }, 'get', () => {
            LogUtil.error(LogUtil.getFileName(),'错误处理1');
            UIManager.ins().hideUILoading();
        })
        httpPromise.then((data: any) => {
            PopupManager.show('prefabs/PopBox', {
                title: '提示',
                content: data.msg,
                confirmCallback: () => {
                    LogUtil.log(LogUtil.getFileName(),'关闭弹窗');
                }
            })
        }).catch(() => {
            LogUtil.error(LogUtil.getFileName(),'错误处理2');
            UIManager.ins().hideUILoading();
        })
    }

    onRealNameConfirm(){
        // PopupManager.show('prefabs/RealNameConfirm', {
        //     confirmCallback: () => {
        //         LogUtil.log(LogUtil.getFileName(),'关闭弹窗');
        //     }
        // })

        //打开本地图片
        let testSp:cc.Sprite = NodeUtil.getChildByName(cc.Canvas.instance.node,'testSp',cc.Sprite);
        BrowserUtil.openLocalFile('.png',(result:any)=>{
            // LogUtil.log('result>>',result);
            // FileUtils.saveForBrowser(result,'llf-test.png');
            let texture = ImageUtil.base64ToTexture(result);
            testSp.spriteFrame = new cc.SpriteFrame(texture);
        });
    }

    onLoadClick() {
        SceneNavigator.go('loadScene', {});
    }


}
